import 'dart:developer';

import 'package:flutter/material.dart';

import '../pages/index_main.dart';

/**
 * 闪屏动画
 */
class OpenAnimation extends StatefulWidget {
  @override
  _OpenAnimationState createState() => _OpenAnimationState();
}

class _OpenAnimationState extends State<OpenAnimation> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation _animation;

  @override
  void initState() {
    super.initState();
    log("initState");
    _controller = AnimationController(duration: Duration(milliseconds: 3000), vsync: this);
    _animation = Tween(begin: 0.0, end: 1.0).animate(_controller);
    _animation.addStatusListener((status) {
      // 动画停止
      if (status == AnimationStatus.completed) {
        Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) {
          // 跳转路由
          return HomePage();
        }), (route) => route == null);
      }
    });
    _controller.forward(); // 播放动画
  }

  @override
  void dispose() {
    super.dispose();
    log("dispose");
    _controller.dispose(); // 停止动画
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: FadeTransition(
      // 淡入动画
      opacity: _animation,
      child: Image.asset(
        "assets/images/splash.jpeg",
        fit: BoxFit.fill,
      ),
    ));
  }
}
